<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>302</title>
</head>
<style type="text/css">
  html,
  body {
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
  }

  body {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #url {
    background: #fff;
    border: 1px solid #dfe1e5;
    box-shadow: none;
    border-radius: 4px;
    z-index: 3;
    padding: 8px 24px;
    width: 600px;
    font-size: 16px;
    line-height: 24px;
  }

  #copy {
    background: #0170fe;
    color: white;
    border: 1px solid #0170fe;
    box-shadow: none;
    padding: 8px 24px;
    border-radius: 4px;
    height: auto;
    cursor: pointer;
    font-size: 16px;
  }

  #show {
    color: rgba(0, 0, 0, .85);
    margin-top: 8px;
    word-break: break-all;
    width: 600px;
    font-size: 16px;
  }
</style>

<body>
  <div id="container">
    <input id="url" placeholder="请输入你要跳转的网址"></input>
    <button id="copy">拷贝</button>
    <div id="show"></div>
  </div>
</body>

<script type="text/javascript">
  function init() {
    const current = new URL(window.location.href);
    const input = document.getElementById('url');

    input.addEventListener('input', e => {
      current.searchParams.set('u', e.target.value);
      document.getElementById('show').innerText = `永久连接: ${current.toString()}`
    });

    const button = document.getElementById('copy');
    button.addEventListener('click', () => {
      document.execCommand('copy');
    });

    document.addEventListener('copy', function (e) {
      e.clipboardData.setData('text/plain', current.toString());
      e.preventDefault();
    });

    for (const queryName of ['u', 'location']) {
      if (current.searchParams.get(queryName)) {
        window.location.href = current.searchParams.get(queryName)
        setTimeout(() => {
          window.close();
        }, 2000);
        return
      }
    }
  }
  init()
</script>

</html>